package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.Employee;
import cn.zyy.hotel.entity.Notice;
import cn.zyy.hotel.service.NoticeService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.vo.NoticeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private RedisTemplate redisTemplate;

    //首页公告渲染
    @RequestMapping("/getNoticeList")
    public void getNoticeList(HttpSession session){
        //将公告放到session中
        List<NoticeVO> noticeList;
        if (redisTemplate.hasKey("noticeList")){
            noticeList = redisTemplate.opsForList().range("noticeList", 0, -1);
        }else {
            noticeList = noticeService.selectNoticeList(null);
            for (NoticeVO noticeVO : noticeList) {
                noticeVO.setCreateDateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(noticeVO.getCreateDate()));
            }
            redisTemplate.opsForList().rightPushAll("noticeList",noticeList);
        }
        session.setAttribute("noticeList",noticeList);
    }

    //查询公告列表
    @RequestMapping("/list")
    public Result list(NoticeVO noticeVO){
        //设置分页信息
        PageHelper.startPage(noticeVO.getPage(),noticeVO.getLimit());
        //调用查询方法
        List<NoticeVO> noticeVOList = noticeService.selectNoticeList(noticeVO);
        //创建分页对象
        PageInfo<NoticeVO> pageInfo = new PageInfo<>(noticeVOList);
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //添加公告
    @RequiresPermissions("notice:add")
    @RequestMapping("/addNotice")
    public Object addNotice(Notice notice,HttpSession session){
        //创建集合保存信息
        Map<String, Object> map = new HashMap<>();
        Employee employee = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        notice.setCreateBy(employee.getId());
        //调用添加公告的方法
        if (noticeService.addNotice(notice) > 0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
            if (redisTemplate.hasKey("noticeList")){
                redisTemplate.delete("noticeList");
            }
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return map;
    }

    //修改公告
    @RequiresPermissions("notice:edit")
    @RequestMapping("/updateNotice")
    public Object updateNotice(Notice notice, HttpSession session){
        //创建集合保存信息
        Map<String, Object> map = new HashMap<>();
        Employee employee = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        notice.setModifyBy(employee.getId());
        //调用添加公告的方法
        if (noticeService.updateNotice(notice) > 0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
            if (redisTemplate.hasKey("noticeList")){
                redisTemplate.delete("noticeList");
            }
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return map;
    }

    //删除公告
    @RequiresPermissions("notice:delete")
    @RequestMapping("/deleteById")
    public Object deleteById(String id){
        //创建集合保存信息
        Map<String, Object> map = new HashMap<>();
        //调用添加公告的方法
        if (noticeService.deleteById(Integer.valueOf(id)) > 0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
            if (redisTemplate.hasKey("noticeList")){
                redisTemplate.delete("noticeList");
            }
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }


}
